#include <sdl/sdl.h>
#include <stdio.h>
#include <stdlib.h>
#include "font.h"
#include "window.h"
#include "screen.h"
#include "EditText.h"

#ifdef main
#undef main
#endif

using namespace std;

int main(int argc,char *argv[]){
    Screen screen = Screen(640,480);
    Font *stdFont = new Font("gfx/bios_font.bmp");
    stdFont->setTransparent(0);
    
    SetStdFont(stdFont);
    
    char done = 0;
    /*
    //save file vars
    char dosave = 0;
    char savename[512];
    memset(savename,0,512);
    int savelength = 0;
    
    //load file vars
    char doload = 0;
    char loadname[512];
    memset(loadname,0,512);
    int loadlength = 0;
    
    char leftDown = 0,rightDown = 0;
    */
    EditText wnd = EditText();

//    map = CreateMap(40,30);
    
    while(!done){
        SDL_Event event;
        while(SDL_PollEvent(&event)){
            switch(event.type){
                case SDL_QUIT:
                    done = 1;
                    break;/*
                case SDL_KEYDOWN:
                    if(event.key.keysym.sym == SDLK_F4){
                        dosave = 1;
                    }else if(event.key.keysym.sym == SDLK_F5){
                        doload = 1;
                    }else if(event.key.keysym.sym == SDLK_PLUS || event.key.keysym.sym == SDLK_KP_PLUS){
                        if(editMode == MODE_TILE){
                            if(currTile < 127) {
                                currTile++;
                            }else currTile = 0;
                        }
                    }else if(event.key.keysym.sym == SDLK_MINUS || event.key.keysym.sym == SDLK_KP_MINUS){
                        if(editMode == MODE_TILE){
                            if(currTile > 0) {
                                currTile--;
                            }else currTile = 127;
                        }
                    }else if(event.key.keysym.sym == SDLK_ESCAPE){
                        if(dosave == 1) dosave = 0;
                        if(doload == 1) doload = 0;
                        
                    }else if(event.key.keysym.sym == SDLK_RETURN){
                        if(dosave == 1) {
                            if(savelength > 0){
                                char path[512];
                                strcpy(path,"map/");
                                strcat(path,savename);
                                strcat(path,".bwm");
                                SaveMap(map,path);
                            }
                            dosave = 0;
                        }else if(doload == 1) {
                            if(loadlength > 0){
                                char path[512];
                                strcpy(path,"map/");
                                strcat(path,loadname);
                                strcat(path,".bwm");
                                
                                strcpy(savename,loadname);
                                savelength = loadlength;
                                
                                map = LoadMap(path);
                            }
                            doload = 0;
                        }
                    }else if(event.key.keysym.sym == SDLK_BACKSPACE){
                        if(dosave == 1) {
                            if(savelength > 0){
                                savelength--;
                                savename[savelength] = 0;  
                            }
                        }else if(doload == 1) {
                            if(loadlength > 0){
                                loadlength--;
                                loadname[loadlength] = 0;  
                            }
                        }
                    }else if(event.key.keysym.sym < 128){
                        if(dosave){
                            if(savelength < 512 && savelength >= 0){
                                savename[savelength] = event.key.keysym.sym;
                                savelength++;
                            }
                        }else if(doload){
                            if(loadlength < 512 && loadlength >= 0){
                                loadname[loadlength] = event.key.keysym.sym;
                                loadlength++;
                            }
                        }
                    }
                    break;
                case SDL_MOUSEMOTION:
                    {
                    int x = event.motion.x;
                    int y = event.motion.y;
                        
                    int off_x = x/16;
                    int off_y = y/16;
                    
                    if(leftDown == 1){
                        if(editMode == MODE_TILE) map->data[off_x+off_y*map->w] = currTile;
                    }else if(rightDown == 1){
                        if(editMode == MODE_TILE) map->data[off_x+off_y*map->w] = 0;
                    }
                    break;
                }
                case SDL_MOUSEBUTTONUP:
                    if(event.button.button == SDL_BUTTON_LEFT){
                        leftDown = 0;
                    }else if(event.button.button == SDL_BUTTON_RIGHT){
                        rightDown = 0;
                    }
                    break;
                case SDL_MOUSEBUTTONDOWN:
                    if(event.button.button == SDL_BUTTON_LEFT){
                        int x = event.button.x;
                        int y = event.button.y;
                        
                        int off_x = x/16;
                        int off_y = y/16;
                        leftDown = 1;
                        //printf("x: %d,y: %d - off_x: %d, off_y: %d\n",x,y,off_x,off_y);
                        
                        if(editMode == MODE_TILE) map->data[off_x+off_y*map->w] = currTile;
                    }else if(event.button.button == SDL_BUTTON_RIGHT){
                        int x = event.button.x;
                        int y = event.button.y;
                        
                        int off_x = x/16;
                        int off_y = y/16;
                        rightDown = 1;
                       // printf("x: %d,y: %d - off_x: %d, off_y: %d\n",x,y,off_x,off_y);
                        
                        if(editMode == MODE_TILE) map->data[off_x+off_y*map->w] = 0;
                    }
                    break;*/
                default:
                    break;
            }
        }
        SDL_FillRect(SDL_GetVideoSurface(),0,0);
        
        //DrawText(100,100,"Hello World!");
        
        wnd.draw();
        
        SDL_UpdateRect(screen.getSurface(),0,0,640,480);  
    }
}




